{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Copyright 2014 Brett Slatkin, Pearson Education Inc.\n",
    "#\n",
    "# Licensed under the Apache License, Version 2.0 (the \"License\");\n",
    "# you may not use this file except in compliance with the License.\n",
    "# You may obtain a copy of the License at\n",
    "#\n",
    "#     http://www.apache.org/licenses/LICENSE-2.0\n",
    "#\n",
    "# Unless required by applicable law or agreed to in writing, software\n",
    "# distributed under the License is distributed on an \"AS IS\" BASIS,\n",
    "# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n",
    "# See the License for the specific language governing permissions and\n",
    "# limitations under the License.\n",
    "\n",
    "# Preamble to mimick book environment\n",
    "import logging\n",
    "from pprint import pprint\n",
    "from sys import stdout as STDOUT"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lock is held\n"
     ]
    }
   ],
   "source": [
    "# Example 1\n",
    "from threading import Lock\n",
    "lock = Lock()\n",
    "with lock:\n",
    "    print('Lock is held')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Lock is held\n"
     ]
    }
   ],
   "source": [
    "# Example 2\n",
    "lock.acquire()\n",
    "try:\n",
    "    print('Lock is held')\n",
    "finally:\n",
    "    lock.release()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 3\n",
    "import logging\n",
    "logging.getLogger().setLevel(logging.WARNING)\n",
    "def my_function():\n",
    "    logging.debug('Some debug data')\n",
    "    logging.error('Error log here')\n",
    "    logging.debug('More debug data')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 4\n",
    "my_function()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 5\n",
    "from contextlib import contextmanager\n",
    "@contextmanager\n",
    "def debug_logging(level):\n",
    "    logger = logging.getLogger()\n",
    "    old_level = logger.getEffectiveLevel()\n",
    "    logger.setLevel(level)\n",
    "    try:\n",
    "        yield\n",
    "    finally:\n",
    "        logger.setLevel(old_level)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Inside:\n",
      "After:\n"
     ]
    }
   ],
   "source": [
    "# Example 6\n",
    "with debug_logging(logging.DEBUG):\n",
    "    print('Inside:')\n",
    "    my_function()\n",
    "print('After:')\n",
    "my_function()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 7\n",
    "with open('my_output.txt', 'w') as handle:\n",
    "    handle.write('This is some data!')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 8\n",
    "@contextmanager\n",
    "def log_level(level, name):\n",
    "    logger = logging.getLogger(name)\n",
    "    old_level = logger.getEffectiveLevel()\n",
    "    logger.setLevel(level)\n",
    "    try:\n",
    "        yield logger\n",
    "    finally:\n",
    "        logger.setLevel(old_level)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 9\n",
    "with log_level(logging.DEBUG, 'my-log') as logger:\n",
    "    logger.debug('This is my message!')\n",
    "    logging.debug('This will not print')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# Example 10\n",
    "logger = logging.getLogger('my-log')\n",
    "logger.debug('Debug will not print')\n",
    "logger.error('Error will print')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.1"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
